package com.ymatou.iapi.smsproxy.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.smsproxy.parameter.MessagePackageBean;
import com.ymatou.iapi.smsproxy.parameter.SendMessageBean;
import com.ymatou.iapi.smsproxy.parameter.SendMessagePackageBean;
import com.ymatou.iapi.smsproxy.service.SendMessageCall;
import com.ymatou.iapi.smsproxy.service.SendMessagePackageCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SendSmsCallService;
import com.ymttest.database.mongo.SmsRecordBean;
import com.ymttest.database.mongo.SmsRecordWapper;
import com.ymttest.utils.TestDataManager;
/**
*发送短信包
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendMessagePackage{
	private static SendMessagePackageBean sendmessagepackageBean;
	private static SendMessagePackageCall sendmessagepackageCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送短信包");
	}

	@Before
	public void caseUp() {
		sendmessagepackageBean=new SendMessagePackageBean();
		sendmessagepackageCall=new SendMessagePackageCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendmessagepackageCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(SendMessagePackageBean sendm,SendMessagePackageCall sendmessageCall) throws Exception
	{
		
		Logger.comment("根据appid、content、phone到数据库表SmsRecord中查询相应记录来验证是否存入SmsRecord表");
		
		List<MessagePackageBean> list = sendm.getMessagePacketList();
		sendmessageCall.getIds().forEach(jid->{
			SmsRecordBean smsRecord = null;
			try {
				smsRecord = new SmsRecordWapper().getCollectionById(jid.getAsString());
				if(smsRecord==null){
					Thread.sleep(5000);
					smsRecord = new SmsRecordWapper().getCollectionById(jid.getAsString());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			Logger.verifyNotNull(smsRecord, "验证数据存入SmsRecord成功");
			Logger.debug(com.ymt.core.tool.FormatUtil.beanToGSONString(smsRecord));
			Logger.verifyEquals(sendm.getAppId(), smsRecord.getAppId(), "验证appid");
			Logger.verifyIsContains(sendm.getMessagePacketList().get(0).getPhone(), smsRecord.getPhones().toString(), "验证phone");
			Logger.verifyIsContains(sendm.getMessagePacketList().get(0).getContext(), smsRecord.getContent(), "验证content");
			Logger.verifyEquals("SendMessagePackage", smsRecord.getApiName(), "验证ApiName");
			Logger.verifyEquals("testSmsDriver", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("testSmsDriver", smsRecord.getChannelName(), "验证ChannelName");
//			if(sendm.getAppId().equals("app-java-teleSign")){
//				Logger.verifyEquals("java-teleSign", smsRecord.getChannelId(), "验证ChannelId");
//				Logger.verifyEquals("testSmsDriver", smsRecord.getChannelName(), "验证ChannelName");
//			}else if(sendm.getAppId().equals("app-java-mw")){
//				Logger.verifyEquals("java-mw", smsRecord.getChannelId(), "验证ChannelId");
//				Logger.verifyEquals("mw", smsRecord.getChannelName(), "验证ChannelName");
//			}else if(sendm.getAppId().equals("app-java-ctc")){
//				Logger.verifyEquals("java-ctc", smsRecord.getChannelId(), "验证ChannelId");
//				Logger.verifyEquals("ctc", smsRecord.getChannelName(), "验证ChannelName");
//			}else if(sendm.getAppId().equals("app-java-qq")){
//				Logger.verifyEquals("java-qq", smsRecord.getChannelId(), "验证ChannelId");
//				Logger.verifyEquals("qq", smsRecord.getChannelName(), "验证ChannelName");
//			}else if(sendm.getAppId().equals("app-java-qq-i")){
//				Logger.verifyEquals("java-qq-i", smsRecord.getChannelId(), "验证ChannelId");
//				Logger.verifyEquals("qq", smsRecord.getChannelName(), "验证ChannelName");
//			}else if(sendm.getAppId().equals("app-java-dl")){
//				Logger.verifyEquals("java-dl", smsRecord.getChannelId(), "验证ChannelId");
//				Logger.verifyEquals("dl", smsRecord.getChannelName(), "验证ChannelName");
//			}
			Logger.verifyNotNull(smsRecord.getCreatedTime(), "验证createdtime");
			Logger.verifyNotNull(smsRecord.getSendedTime(), "验证SendedTime");
			Logger.verifyNotNull(smsRecord.getClientIp(), "验证ClientIp");
			Logger.verifyNotNull(smsRecord.getServerIp(), "验证ServerIp");
			Logger.verifyEquals(1, smsRecord.getNumber(), "验证Number");
			//TODO MessageAction 6成功 7失败 8 黑名单
//			Logger.verifyEquals("java app", smsRecord.getComment(), "验证Comment");
			Logger.verifyIsContains(smsRecord.get_id(),sendmessageCall.getString("result"), "验证result的id数据一致");
		});
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_001() {
		Logger.start(true,"发送短信包app-java-ctc成功");
		try {
			sendmessagepackageBean.setAppId("app-java-ctc");
			
			List<MessagePackageBean> messagePackagelist = new ArrayList<MessagePackageBean>();
			
			MessagePackageBean messagePackageBean = new MessagePackageBean();
			messagePackageBean.setPhone(SendSmsCallService.TESTPHONE);
			messagePackageBean.setContext("自动化测试_短信包发送_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean);
			
			sendmessagepackageBean.setMessagePacketList(messagePackagelist);
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
			Logger.verifyEquals("success",sendmessagepackageCall.getString("errorMessage"),"验证errorMessage");
			Logger.verifyEquals("0",sendmessagepackageCall.getString("errorCode"),"验证errorCode");
			Logger.verifyEquals("true",sendmessagepackageCall.getString("success"),"验证success");
			Thread.sleep(1000);
			verifyDB(sendmessagepackageBean, sendmessagepackageCall);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}